#include "boxman.h"
#include <stdio.h>
#include <stdarg.h>

const char gamename[] = "Boxman";

int error(char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    vfprintf(stderr, format, ap);
    exit(1);
}

char arrow_key[] = {'w', 'a', 's', 'd'};
int arrow_off[][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const size_t NARROW = 4;

int main()
{
    FILE *fin;
    int key;
    int level = 0;
    int step = 0;

    if ((fin = fopen("boxman.txt", "r")) == NULL)
        error("No such file: boxman.txt\n");
    if (readin(fin))
        error("Bad file: boxman.txt\n");
    clearScreen();
    while (1){
        drawall(level, step);
        key = readChar();
        if (key == 'p')
            return 0;
        else{
            int i;
            for (i = 0; i < NARROW; i++){
                if (arrow_key[i] == key){
                    if (moveman(arrow_off[i][0], arrow_off[i][1]))
                        step++;
                    if (nbox == nbox_ontarget){
                        if (readin(fin))
                            return 0;
                        step = 0;
                        clearScreen();
                        drawall(++level, step);
                    }
                    break;
                }
            }
        }
    }
    return 0;
}

